package com.xlhl.init.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xlhl.init.anno.MustLogin;
import com.xlhl.init.common.Result;
import com.xlhl.init.common.Validatable;
import com.xlhl.init.exceptipn.BusinessException;
import com.xlhl.init.model.entity.Account;
import com.xlhl.init.service.AccountService;
import com.xlhl.init.simple.querytype.SimpleInterface;
import com.xlhl.init.simple.querytype.SimpleRequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试接口 只在 dev 环境下加载
 *
 * @author xlhl
 */
@RestController
@RequestMapping("/test")
@Profile(value = "dev")
@Slf4j
public class TestController {

    //  region Sa-Token 测试


    @GetMapping("/login")
    public Result<String> test() {

        StpUtil.login(1008611);

        return Result.success(StpUtil.getLoginType());
    }

    @GetMapping("/logout")
    public Result<String> test1() {

        StpUtil.logout(1008611);

        return Result.success();
    }

    @GetMapping("kickout")
    public Result<String> test2() {
        StpUtil.kickout(1008611);

        return Result.success();
    }

    @GetMapping("/isLogin")
    public Result<String> test3() {
        return Result.success(StpUtil.isLogin() ? "当前会话已登录: " + StpUtil.getLoginId() : "未登录");
    }

    @GetMapping("/hasPermission")
    public Result<String> test4() {
        StpUtil.checkPermission("user.add");
        return Result.success(StpUtil.isLogin() ? "当前会话已登录: " + StpUtil.getLoginId() : "未登录");
    }

    @GetMapping("/check")
    public Result<String> test5() {
        try {
            StpUtil.checkLogin();
        } catch (NotLoginException e) {
            String type = e.getType();
            System.out.println(type);
            throw new BusinessException(e.getMessage());
        }
        return Result.success();
    }

    //  endregion

    //  region Validatable 自动参数校验

    @GetMapping("/success")
    public Result<String> test6(TestSuccessRequestBody body) {

        return Result.success();
    }

    @GetMapping("/exception")
    public Result<String> test7(TestExceptionRequestBody body) {
        return Result.success();
    }

    @GetMapping("/error")
    public Result<String> test8() {
        throw new RuntimeException("这样是不会被返回给前端哦");
    }

    public static class TestSuccessRequestBody implements Validatable {

        @Override
        public void doValidate() throws BusinessException {
            log.info("参数自动校验了哦~");
        }
    }

    public static class TestExceptionRequestBody implements Validatable {

        @Override
        public void doValidate() throws BusinessException {
            log.error("我要开始报错了");
            throw new BusinessException("我要开始报错了");
        }
    }

    //  endregion

    //  region 测试 MustLogin 必须登录

    /**
     * 登录用上面 Sa-Token 测试用过的
     *
     * @return
     */
    @GetMapping("/mustLogin")
    @MustLogin
    public Result<String> test9() {
        return Result.success("很好，你已经登录了");
    }

    //  endregion


    @Resource
    private SimpleInterface simpleInterface;

    @Resource
    private AccountService accountService;

    /**
     * {@link com.xlhl.init.anno.QueryType} 使用案例
     * <p>
     * <p>
     * Preparing: SELECT COUNT(*) AS total FROM account WHERE deleted = 0 AND (username LIKE ? AND password = ? AND create_time >= ? AND create_time <= ?)
     * </p>
     * <p>
     * Parameters: %456%(String), 123(String), 2020-01-01(String), 2021-01-01(String)
     * </p>
     *
     * @param body
     * @return
     */
    @PostMapping("/page")
    public Result<Page<Account>> listAccountByPage(@RequestBody SimpleRequestBody body) {
        QueryWrapper<Account> queryWrapper = simpleInterface.getQueryWrapperFilterByNotSelect(body, Account.class);

        return Result.success(accountService.page(new Page<>(body.getCurrent(), body.getPageSize()), queryWrapper));
    }

    /**
     * {@link com.xlhl.init.anno.QueryType} 使用案例
     * <p>
     * <p>
     * Preparing: SELECT id,username,password,nickname,phone,role_id,email,avatar,last_login_time,last_login_ip,status,department,creator_id,creator_name,create_time,update_time,edit_time FROM account WHERE deleted=0 AND (username LIKE ? AND password = ?) ORDER BY create_time DESC
     * <p>
     * Parameters: %%(String), (String)
     * </p>
     *
     * @param body
     * @return
     */
    @PostMapping("/list")
    public Result<List<Account>> listAccount(@RequestBody SimpleRequestBody body) {
        QueryWrapper<Account> queryWrapper = simpleInterface.getQueryWrapperFilterByNotSelect(body, Account.class);

        return Result.success(accountService.list(queryWrapper));
    }
}













